Function SendMail([string]$CurMemUse)
{
    $PSEmailServer = "smtp.supor.com"
    $SMTPPort = 2525
    $SMTPUsername = "bpmmon@supor.com"
    $SecureStringPassword = ConvertTo-SecureString "1231@supor" -AsPlainText -Force
    $EmailCredential = New-Object -TypeName System.Management.Automation.PSCredential `
							      -ArgumentList $SMTPUsername,$SecureStringPassword
    ####################### Now Sending E-Mail ... ... #########################
    $MailTo = "ljquan@supor.com","376918528@qq.com"
    $MailFrom = "bpmmon@supor.com"
    $MailSubject = "BPM Application Server MEMORY WARNING!"
    $MailBody = "`t`t`t BPM Application Server `
                 Hostname: HAH45VM.cn.supor.corp `
                 IP Address: 172.24.1.136 `
                 Current MEMORY Usage:  [" + $CurMemUse + "] `
                 Please take action immediately !!!"

    Send-MailMessage -From $MailFrom -To $MailTo `
				     -Subject $MailSubject -Body $MailBody `
				     -Port $SMTPPort `
				     -Credential $EmailCredential
} 


<#
 get memory physical capacity & 
 available capacity
 and then calculate memory usage percent.
 #>
$MEM_Capacity=((get-wmiobject -class Win32_PhysicalMemory `
				-namespace "root\cimv2").Capacity)/1024/1024
$MEM_Available=(get-wmiobject -class Win32_PerfFormattedData_PerfOS_Memory `
				-namespace "root\cimv2").AvailableMBytes
$MEM_Usage = (($MEM_Capacity-$MEM_Available)/$MEM_Capacity)*100

if($MEM_Usage -gt 6.0)
{
    $MEM_Usage_Percent = "{0:N1}" -f $MEM_Usage + "%"
    #Write-Host $MEM_Usage_Percent
    SendMail($MEM_Usage_Percent)
}


<#
 Usage:
 Phase#1 creates a job trigger that runs a scheduled job once 
         every [1] Minute for an indefinite period of time
         -------------------------------------------------------------
 > $T = New-JobTrigger -Once -At "4/16/2019 9pm" `
					-RepetitionInterval (New-TimeSpan -Minutes 1) `
					-RepetitionDuration ([TimeSpan]::MaxValue)

 Phase#2 creates the 'BPMMON' scheduled job, 
         which runs the bpmmon.ps1 script every [1] minute 
         The command uses the *FilePath* parameter to specify the script that the job runs. 
         It uses the *Trigger* parameter to specify the job triggers in the $T variable 
         ---------------------------------------------------------------------------------
 > Register-ScheduledJob -Name BPMMON -Trigger $T `
                         -FilePath C:\Users\Administrator\desktop\bpmmon.ps1 
 #>